﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AsyncCamera.Camera.Structure;
using AsyncCamera.Camera.Pelco;
using System.Windows;

namespace Incendios0_1.Camera.wpfdatamanager
{
    class ContextMenuFactory
    {
        public static ContextMenu createContextMenu()
        {
            ContextMenu cox = new ContextMenu();
            return cox;
        }

        public static LinkedMenuItem<AbstractCamera>[] createMenuItems(AbstractCamera camera)
        {
            List<LinkedMenuItem<AbstractCamera>> items = new List<LinkedMenuItem<AbstractCamera>>();
            LinkedMenuItem<AbstractCamera> current;
            LinkedMenuItem<AbstractCamera> state = null;
            if (camera == null)
            {
                current = new LinkedMenuItem<AbstractCamera>("Camara: None");
                state = new LinkedMenuItem<AbstractCamera>("Estado: Parado");
                
            }
            else
            {
                current = new LinkedMenuItem<AbstractCamera>("Camara: " + camera.cameraData.HWControlAddress.ToString());
                switch (camera.media.CurrentState)
                {
                    case MediaState.Pause:
                        state = new LinkedMenuItem<AbstractCamera>("Estado: Pausa");
                        break;
                    case MediaState.Play:
                        state = new LinkedMenuItem<AbstractCamera>("Estado: Reproducir");
                        break;
                    case MediaState.Stop:
                        state = new LinkedMenuItem<AbstractCamera>("Estado: Parado");
                        break;
                }
            }
            current.Enabled = false;
            state.Enabled = false;
            current.Link = camera;
            state.Link = camera;
            items.Add(current);
            //items.Add(state);
            items.Add(new LinkedMenuItem<AbstractCamera>("-",camera));
            items.Add(new LinkedMenuItem<AbstractCamera>("Reset", new EventHandler(Reset), camera));
            items.Add(new LinkedMenuItem<AbstractCamera>("Control de Camara", new EventHandler(OpenPanel),camera));
            LinkedMenuItem<AbstractCamera> camaras = new LinkedMenuItem<AbstractCamera>("Camaras:",camera);
            /*items.Add(camaras);
            foreach (CameraData cam in CameraManager.CamerasData)
            {
                if (camera == null || !cam.Name.Equals(camera.cameraData.Name))
                {
                    camaras.MenuItems.Add(new LinkedMenuItem<AbstractCamera>(cam.Name, new EventHandler(ChangeCameraClick),camera));
                }
            }*/
            
            return items.ToArray();
        }

        public static void ChangeCameraClick(object sender, System.EventArgs e)
        {

        }

        public static void OpenPanel(object sender, EventArgs e)
        {
            LinkedMenuItem<AbstractCamera> item = (LinkedMenuItem<AbstractCamera>) sender;
            AbstractCamera cam = item.Link;
            if (cam is PelcoCamera)
                new CameraManualControlW((PelcoCamera)cam).Show();
            else
                Microsoft.Windows.Controls.MessageBox.Show("La camara no soporta control manual", "Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
        }

        public static void Reset(object sender, EventArgs e)
        {
            LinkedMenuItem<AbstractCamera> item = (LinkedMenuItem<AbstractCamera>)sender;
            AbstractCamera cam = item.Link;
            cam.resetDetection();
            CameraManager.windowMap.listBox1.Items.Remove(cam);
        }
    }
}
